import sponsorApi from 'api/sponsor'

// state
let state = {
  sponsor_dict: {
    match: {url: '/sponsor/mer/match/list/select', data: []}, // 赛事列表
    dictBase: {
      url: 'sponsor/dict/base',
      data: {
        mediaExpRights: [],
        networkMedia: [],
        ntellectualRights: [],
        outdoorMedia: [],
        planeMedia: [],
        raceAdRights: [],
        receptionRights: [],
        socialMedia: [],
        tvStation: []
      }
    },
    sportType: {url: '/sponsor/dict/sport/type', data: []}, // 投放项目类型
    brandList: {url: '/sponsor/plan/brand/simple/select', data: []}, // 赞助品牌列表
    sponsorType: {data: [ // 赞助方式
      {value: 0, label: '现金'},
      {value: 1, label: '物资'},
      {value: 2, label: '现金+物资'}
    ]},
    targetGroup: {data: [ // 目标人群
      {value: 0, label: '全部'},
      {value: 1, label: '母婴'},
      {value: 2, label: '儿童'},
      {value: 3, label: '青少年'},
      {value: 4, label: '青年'},
      {value: 5, label: '中年'},
      {value: 6, label: '老年'}
    ]},
    sponsorPurpose: {data: [ // 赞助目的
      {value: 0, label: '不限'},
      {value: 1, label: '销售'},
      {value: 2, label: '品牌'}
    ]}
  },
  sponsor_business_list: {list: [], loaded: false},
  sponsor_brand_list: {list: [], loaded: false},
  sponsor_talk_list: {list: [], loaded: false},
  sponsor_support_list: {list: [], loaded: false}
}

// getters
const getters = {
  sponsor_brand_list: state => state.sponsor_brand_list,
  sponsor_business_list: state => state.sponsor_business_list,
  sponsor_dict: state => state.sponsor_dict,
  sponsor_talk_list: state => state.sponsor_talk_list,
  sponsor_support_list: state => state.sponsor_support_list

}
// actions
const actions = {
  sponsor_brand_list ({ commit, state }, params) { // 品牌列表
    sponsorApi.brand_list(params).then((json) => {
      commit('SPONSOR_BRAND_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  sponsor_brand_del ({ commit, state }, params) { // 品牌删除
    sponsorApi.brand_del(params).then((json) => {
      console.log(json, 22222222)
      return true
    })
  },
  sponsor_brand_edit ({ commit, state }, params) { // 品牌编辑
    sponsorApi.brand_add(params).then((json) => {
      console.log(json)
    })
  },
  sponsor_business_list ({ commit, state }, params) { // 招商计划列表
    sponsorApi.business_list(params).then((json) => {
      commit('SPONSOR_BUSINESS_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  sponsor_business_changestate ({ commit, state }, params) { // 招商计划修改状态
    let data = {
      id: params.row.id,
      status: params.row.status
    }
    sponsorApi.business_changestate(data).then((json) => {
      commit('SPONSOR_BUSINESS_CHANGESTATE', {
        data: true,
        params: params
      })
    }, () => {
      commit('SPONSOR_BUSINESS_CHANGESTATE', {
        data: false,
        params: params
      })
    })
  },
  sponsor_dict ({ commit, state }, params) { // 新增招商计划 字典数据
    let url = state.sponsor_dict[params].url
    sponsorApi.dict(url).then((json) => {
      commit('SPONSOR_DICT', {
        data: json,
        params: params
      })
    })
  },
  sponsor_talk_list ({ commit, state }, params) { // 约谈信息列表
    sponsorApi.talk_list(params).then((json) => {
      commit('SPONSOR_TALK_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  sponsor_support_list ({ commit, state }, params) { // 赞助计划列表
    sponsorApi.support_list(params).then((json) => {
      commit('SPONSOR_SUPPORT_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  sponsor_support_changestate ({ commit, state }, params) { // 招商计划修改状态
    console.log(params.row.status)
    let data = {
      id: params.row.id,
      status: params.row.status
    }
    sponsorApi.support_changestate(data).then((json) => {
      commit('SPONSOR_SUPPORT_CHANGESTATE', {
        data: true,
        params: params
      })
    }, () => {
      commit('SPONSOR_SUPPORT_CHANGESTATE', {
        data: false,
        params: params
      })
    })
  }
}
// mutations
const mutations = {
  SPONSOR_BRAND_LIST (state, { data, params }) {
    state.sponsor_brand_list = data
  },
  SPONSOR_BUSINESS_LIST (state, { data, params }) { // 招商列表
    if (data && data.list) {
      data.list.map(i => {
        i.status = i.status.toString() // 开关状态值，vue组件开关识别字符串
      })
      state.sponsor_business_list = data
    }
    state.sponsor_business_list.loaded = true // 判断第一次loading
  },
  SPONSOR_BUSINESS_CHANGESTATE (state, { data, params }) { // 招商计划 修改状态
    state.sponsor_business_list.list[params.$index].status = data ? (params.row.status) : (params.row.status === '0' ? '1' : '0')
  },
  SPONSOR_DICT (state, { data, params }) { // 通用数据字典方法
    let newjson = []
    let named = (data, value, label, isInt) => { // key值处理方法
      let json = []
      for (let i in data) {
        json[i] = {
          value: isInt ? data[i][value] : '' + data[i][value],
          label: data[i][label]
        }
      }
      return json
    }
    if (params === 'match') { // 单独处理赛事列表接口数据
      newjson = named(data.data, 'matchId', 'matchName', true)
    } else if (data.data) {
      if (data.data instanceof Array) {
        if (data.data[0] && data.data[0].fieldValue) {
          newjson = named(data.data, 'fieldValue', 'fieldName', true)
        } else {
          newjson = data.data
        }
      } else {
        for (var i in data.data) {
          if (i === 'tvStation') { // 判断是否多层级数组
            newjson[i] = []
            for (let j in data.data[i]) {
              newjson[i].push({
                label: data.data[i][j].label
              })
              // newjson[i][j].data = named(data.data[i][j].data, 'fieldValue', 'fieldName')
              newjson[i][j].data = data.data[i][j].data
            }
          } else {
            if (data.data[i] instanceof Array) {
              newjson[i] = data.data[i]
              // newjson[i] = named(data.data[i], 'fieldValue', 'fieldName')
            }
          }
        }
      }
    }
    state.sponsor_dict[params].data = newjson
  },
  SPONSOR_TALK_LIST (state, { data, params }) { // 约谈列表接口
    if (data.list) {
      state.sponsor_talk_list = data
    }
    state.sponsor_talk_list.loaded = true
  },
  SPONSOR_SUPPORT_LIST (state, { data, params }) { // 赞助计划 列表接口
    if (data && data.list) {
      data.list.map(i => {
        i.status = i.status.toString()
      })
      state.sponsor_support_list = data
    }
    state.sponsor_support_list.loaded = true
  },
  SPONSOR_SUPPORT_CHANGESTATE (state, { data, params }) { // 赞助计划 修改状态
    state.sponsor_support_list.list[params.$index].status = data ? (params.row.status) : (params.row.status === '0' ? '1' : '0')
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
